package cn.jimmie.lib.qlog.support;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Pair;

/**
 * function :
 *
 * <p> Create Time:  2019年04月28日 17:35 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public final class AlDevice {
    private static Pair<Integer, Integer> sResolution = null;
    private static String sAndroidId = AlString.NULL;
    private static String sImei = AlString.NULL;
    private static String sImsi = AlString.NULL;

    /**
     * 获取分辨率
     */
    public static Pair<Integer, Integer> getResolution(Context ctx) {
        if (ctx == null || sResolution != null) return sResolution;
        DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
        sResolution = new Pair<>(dm.widthPixels, dm.heightPixels);
        return sResolution;
    }

    /**
     * AndroidId
     */
    public static String getAndroidId(Context ctx) {
        if (ctx == null || !sAndroidId.isEmpty()) return sAndroidId;

        try {
            @SuppressLint("HardwareIds")
            String android_id = android.provider.Settings.Secure.getString(
                    ctx.getApplicationContext().getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
            sAndroidId = AlString.nonNull(android_id);
        } catch (Throwable ignored) {
        }
        return sAndroidId;
    }

    /**
     * 获取电话卡id
     */
    public static String getImei(Context ctx) {
        if (ctx == null || !sImei.isEmpty()) return sImei;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            sImei = getAndroidId(ctx);
            return sImei;
        }

        if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED)
            return AlString.NULL;
        try {
            TelephonyManager manager = getTelephonyManager(ctx);
            @SuppressLint("HardwareIds") String imei = manager.getDeviceId();
            sImei = AlString.nonNull(imei);
        } catch (Throwable ignored) {
        }
        return sImei;
    }

    public static String getImsi(Context ctx) {
        if (ctx == null || !sImsi.isEmpty() ||
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            return sImsi;

        if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED)
            return AlString.NULL;
        try {
            TelephonyManager telephonyManager = getTelephonyManager(ctx);
            @SuppressLint("HardwareIds") String imsi = telephonyManager.getSubscriberId();
            sImsi = AlString.nonNull(imsi);
        } catch (Throwable ignored) {
        }
        return sImsi;
    }


    private static TelephonyManager getTelephonyManager(Context ctx) {
        return (TelephonyManager) ctx.getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
    }
}
